﻿using EYangFrame.DataAccess.Global;
using EYangFramewirk.Business.MessageSystem.Interfaces;
using EYangFramework.Domain.Entitys.Entrys;
using EYangFramework.Domain.Entitys.ValueObjects;
using EYangFramework.Domain.Repositorys;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EYangFramewirk.Business.MessageSystem.Implementation
{
    public class BLLSendMessage : IBLLSendMessage
    {
        private IRepository<Message> RepositoryMessage { get; set; }
        private IRepository<MessageModel> RepositoryMessageModel { get; set; }
        private IRepository<Role> RepositoryRole { get; set; }
        private IRepository<LinkUser> RepositoryLinkUser { get; set; }
        private IRepository<User> RepositoryUser { get; set; }
        public BLLSendMessage(IRepository<Message> repositoryMessage,
            IRepository<MessageModel> repositoryMessageModel, 
            IRepository<Role> repositoryRole, 
            IRepository<LinkUser> repositoryLinkUser,
            IRepository<User> repositoryUser)
        {
            this.RepositoryMessage = repositoryMessage;
            this.RepositoryMessageModel = repositoryMessageModel;
            this.RepositoryRole = repositoryRole;
            this.RepositoryLinkUser = repositoryLinkUser;
            this.RepositoryUser = repositoryUser;
        }
        public void Dispose()
        {
            RepositoryMessage.DbContext.Dispose();
            RepositoryMessageModel.DbContext.Dispose();
            RepositoryRole.DbContext.Dispose();
        }
        public List<Message> GetMessages(string[] currentUsers)
        {
            List<Message> messages = RepositoryMessage.GetAllNoTracking().Where(t => System.Data.Entity.DbFunctions.DiffDays(DateTime.Now, t.CREATETIME) <= 5).ToList();
            return messages.Where(t => IsCommunicationUser(t, currentUsers)).OrderBy(t => t.CREATETIME).ToList();
        }
        private bool IsCommunicationUser(Message message, string[] currentUsers)
        {
            string[] users = (message.From + "," + message.To).Split(',');
            if (users.Length != currentUsers.Length)
            {
                return false;
            }
            foreach (var item in users)
            {
                if (!currentUsers.Contains(item))
                {
                    return false;
                }
            }
            return true;
        }

        public Message GetMessageByID(Guid id)
        {
            return RepositoryMessage.GetAllNoTracking().Where(t => t.ID == id).First();
        }

        public List<MessageModel> GetMessageModel()
        {
            return RepositoryMessageModel.GetAllListNoTracking(t => t.User == GlobalInfo.CurrentUser.USERNAME).ToList();
        }

        public List<Role> GetRoles()
        {
            return RepositoryRole.GetAllNoTracking().Include(t=>t.USERS).ToList();
        }

        public List<LinkUser> GetLinkUsers()
        {
            var id = GlobalInfo.CurrentUser.ID.ToString();
            return RepositoryLinkUser.GetAllListNoTracking(t => t.User == id);
        }

        public bool SendMessage(Message message)
        {
            RepositoryMessage.Insert(message);
            return true;
        }

        public List<User> GetUsers()
        {
            return RepositoryUser.GetAllListNoTracking(t => !t.ISDELETE).ToList();
        }

        public MessageModel GetModelByName(string Name)
        {
            return RepositoryMessageModel.GetAllListNoTracking(t => t.ModelName == Name).First();
        }
    }
}
